package com.example.panker.panker.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.example.panker.panker.R;
import com.example.panker.panker.ui.activity.Activity_main;
import com.example.panker.panker.ui.activity.Activity_web;
import com.example.panker.panker.uilt.Tools.NetUtils;
import com.example.panker.panker.uilt.Tools.TittleManager;
import com.example.panker.panker.uilt.rollviewpager.OnItemClickListener;
import com.example.panker.panker.uilt.rollviewpager.RollPagerView;
import com.example.panker.panker.uilt.rollviewpager.adapter.LoopPagerAdapter;
import com.example.panker.panker.uilt.rollviewpager.hintview.IconHintView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by Ivory on 2016/7/19.
 * 天才兜儿又来啦！！！
 * hahahaha
 */
public class news extends basefragment implements View.OnClickListener {
    private View mContent;

    private RollPagerView mLoopViewPager;
    private TestLoopAdapter mLoopAdapter;
    private Handler handler = new Handler();
    int mPage = 3;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContent=inflater.inflate(R.layout.fragment_news,container,false);
        mLoopViewPager= (RollPagerView) mContent.findViewById(R.id.loop_view_pager);
        return mContent;
    }

    @Override
    protected void initEvent() {
        mLoopViewPager.setPlayDelay(1000);
        mLoopViewPager.setAdapter(mLoopAdapter = new TestLoopAdapter(mLoopViewPager));
        mLoopViewPager.setHintView(new IconHintView(mActivity,R.drawable.point_focus,R.drawable.point_normal));
        mLoopViewPager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(mActivity,"Item "+position+" clicked",Toast.LENGTH_SHORT).show();
                Intent i=new Intent(mActivity,Activity_web.class);
                switch (position) {
                    case 0:
                        i.putExtra("url", "http://mp.weixin.qq.com/s/t0uWfNgb_KZdLmP08LRxvg");
                        break;
                    case 1:
                        i.putExtra("url", "http://mp.weixin.qq.com/s/0GTUed75a5vQJW5gNMfQYA");
                        break;
                    case 2:
                        i.putExtra("url", "http://mp.weixin.qq.com/s/Zv_8mvf5lA7ZvIVQu6kMPQ");
                        break;
                }
                startActivity(i);
            }
        });
        getData(mPage);
    }

    @Override
    public void onClick(View view) {

    }

    public void getData(final int page){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String content = NetUtils.get("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/"+3+"/"+page);
                if (TextUtils.isEmpty(content)){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mActivity,"网络连接出错",Toast.LENGTH_SHORT).show();
                        }
                    });
                    return;
                }
                try {
                    System.out.println("hhhhhhhhhhhhhhhhhhhhhhh");
                    JSONObject jsonObject = new JSONObject(content);
                    JSONArray strArr = jsonObject.getJSONArray("results");
                    final String[] imgs  = new String[strArr.length()];
                    for (int i = 0; i < strArr.length(); i++) {
                        JSONObject obj = strArr.getJSONObject(i);
                        imgs[i] = obj.getString("url");
                    }
                    Log.i("MainActivity","new data:"+imgs.length);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            mLoopAdapter.setImgs(imgs);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private class TestLoopAdapter extends LoopPagerAdapter {
        String[] imgs = new String[0];

        public void setImgs(String[] imgs){
            this.imgs = imgs;
            notifyDataSetChanged();
        }


        public TestLoopAdapter(RollPagerView viewPager) {
            super(viewPager);
        }

        @Override
        public View getView(ViewGroup container, int position) {
            Log.i("RollViewPager","getView:"+imgs[position]);

            ImageView view = new ImageView(container.getContext());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("RollViewPager","onClick");
                }
            });
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            Glide.with(mActivity)
                    .load(imgs[position])
                    // .placeholder(R.drawable.img4)
                    // .error(R.drawable.img1)
                    .into(view);
            return view;
        }

        @Override
        public int getRealCount() {
            return imgs.length;
        }

    }
}